package com.unbox.common.swagger;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;

@ConditionalOnProperty("swagger.title")
@EnableOpenApi
public class UnboxSwagger3Configuration {

    @Value("${swagger.title}")
    String title;
    @Value("${swagger.description}")
    String description;
    @Value("${swagger.name}")
    String name;
    @Value("${swagger.url}")
    String url;
    @Value("${swagger.email}")
    String email;
    @Value("${swagger.version}")
    String version;
    @Value("${swagger.pkg}")
    String pkg;
    @Value("${swagger.enable}")
    Boolean enable;

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.OAS_30)
                .enable(enable)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage(pkg))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                .description(description)
                .contact(new Contact(name, url, email))
                .version(version)
                .build();
    }
}
